/*
 * @lc app=leetcode.cn id=160 lang=javascript
 *
 * [160] 相交链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} headA
 * @param {ListNode} headB
 * @return {ListNode}
 */
var getIntersectionNode = function(headA, headB) {
  if (!headA || !headB) return null
  let pointA = headA
  let pointB = headB
  while (pointA !== pointB) {
    // 先对齐两个链表
    pointA = !pointA ? headB : pointA.next
    pointB = !pointB ? headA : pointB.next
    if (!pointA && !pointB) return null
  }
  return pointA
};
// @lc code=end

